<template>
  <div class="mod-config">
    <!-- 操作按钮 -->
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-button
          v-if="isAuth('news:newsCategory:save')"
          type="primary"
          @click="addOrUpdateHandle()">新增</el-button>
      </el-form-item>
    </el-form>

    <!-- 表格 -->
    <el-table
      :data="dataList"
      row-key="id"
      border
      v-loading="dataListLoading">
        <el-table-column type="index" align="center" label="序号" />
        <el-table-column prop="name" align="center" label="分类名" />
        <el-table-column prop="updateTime" align="center" label="更新时间" />
        <el-table-column prop="sort" align="center" label="排序" />

        <el-table-column
          label="是否显示"
          header-align="center"
          align="center">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.isDisplay === 0">隐藏</el-tag>
            <el-tag v-else-if="scope.row.isDisplay === 1" type="success">显示</el-tag>
          </template>
        </el-table-column>

        <el-table-column
          fixed="right"
          header-align="center"
          align="center"
          width="150"
          label="操作">
          <template slot-scope="scope">
            <el-button type="text" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
            <el-button type="text" @click="deleteHandle(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>

    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
  import TableTreeColumn from '@/components/table-tree-column'
  import AddOrUpdate from './news-category-add-or-update'

  export default {
    data () {
      return {
        dataForm: {
          key: ''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        addOrUpdateVisible: false
      }
    },
    components: {
      TableTreeColumn,
      AddOrUpdate
    },
    mounted () {
      this.getDataList()
    },
    methods: {
      // 获取数据列表
      async getDataList () {
        this.dataListLoading = true
        var res = await this.$http.post('/news-admin/category/list',
          {
            'page': this.pageIndex,
            'limit': this.pageSize
          })

        if (res && res.code === 200) {
          this.dataList = res.data.list
          this.totalPage = res.data.totalCount
        } else {
          this.dataList = []
          this.totalPage = 0
          this.pageIndex = 1
        }
        this.dataListLoading = false
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => this.$refs.addOrUpdate.init(id))
      },
      // 删除
      async deleteHandle (id) {
        var confirm = await this.$prompt(`您确认删除分类及相关新闻吗，请输入DEL确认您的操作？`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          inputPattern: /^del$/i,
          inputErrorMessage: '请输入DEL确认您的操作'
        })
        if (confirm.action !== 'confirm') return

        let res = await this.$http.post('/news-admin/category/delete',
          {
            'id': id,
            'confirm': confirm.value
          })

        if (res && res.code === 200) {
          this.$notify.success({title: '成功', message: '删除新闻分类成功'})
          this.pageIndex > 1 && this.dataList.length <= 1 && this.pageIndex --
          this.getDataList()
        } else {
          this.$notify.error({title: '错误', message: res.msg})
        }
      }
    }
  }
</script>
